package com.itheima.health.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.dao.RoleDao;
import com.itheima.health.dao.RolePermissionDao;
import com.itheima.health.dao.UserRoleDao;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.exception.CustomizeException;
import com.itheima.health.pojo.Role;
import com.itheima.health.pojo.RolePermission;
import com.itheima.health.pojo.User;
import com.itheima.health.pojo.UserRole;
import com.itheima.health.service.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Slf4j
@DubboService
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleDao roleDao;
    @Autowired
    private RolePermissionDao rolePermissionDao;
    @Autowired
    private UserRoleDao userRoleDao;

    @Override
    public Role findById(Integer id) {
        Role newRole = new Role();
        newRole.setId(id);
        Role role = roleDao.selectOne(newRole);
        log.info("[role表-findById-返回值] - role: {}",role);
        return role;
    }

    @Override
    public List<Role> findAll() {
        List<Role> roleList = roleDao.selectAll();
        log.info("[role表-findAll-返回值] - roleList: {}",roleList);
        return roleList;
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        Page<User> page = roleDao.findPage(queryPageBean.getQueryString());
        log.info("[role表-findPage-返回值] - page: {}",page);
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Transactional
    @Override
    public boolean add(Role role, Integer[] pIds) {
        log.info("[role表-add-接收值] - role: {} pIds: {}",role,pIds);
        boolean flag = false;
        roleDao.insertSelective(role);
        Integer roleId = role.getId();
        RolePermission rolePermission = new RolePermission();
        rolePermission.setRoleId(roleId);
        for (Integer pId : pIds) {
            rolePermission.setPermissionId(pId);
            rolePermissionDao.insertSelective(rolePermission);
            flag = true;
        }
        return flag;
    }

    @Transactional
    @Override
    public boolean update(Role role, Integer[] pIds) {
        log.info("[role表-update-接收值] - role: {} - pIds: {}",role,pIds);
        boolean flag = false;
        int updateCount = roleDao.updateByPrimaryKeySelective(role);
        Integer roleId = role.getId();
        Example example = new Example(RolePermission.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("roleId",roleId);
        rolePermissionDao.deleteByExample(example);

        if (pIds.length>0){
            RolePermission rolePermission = new RolePermission();
            rolePermission.setRoleId(roleId);
            for (Integer permissionId : pIds) {
                rolePermission.setPermissionId(permissionId);
                rolePermissionDao.insertSelective(rolePermission);
            }
            flag = true;
        }
        if (updateCount > 0) {
            flag = true;
        }
        return flag;
    }

    @Override
    public boolean deleteById(Integer id) {
        log.info("[role表-deleteById-接收值] - id: {}",id);
        boolean flag = false;
        Example example = new Example(RolePermission.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("roleId",id);
        List<RolePermission> rolePermissions = rolePermissionDao.selectByExample(example);
        if (rolePermissions.size()>0){
            throw new CustomizeException("删除失败，该角色关联的有对应权限，请先删除对应的权限");
        }

        Example userExample = new Example(UserRole.class);
        Example.Criteria userCriteria = userExample.createCriteria();
        userCriteria.andEqualTo("roleId",id);
        List<UserRole> userRoleList = userRoleDao.selectByExample(userExample);
        if (userRoleList.size()>0){
            throw new CustomizeException("删除失败，该角色关联的有对应用户，请先删除对应的用户");
        }

        int deleteCount = roleDao.deleteByPrimaryKey(id);
        if (deleteCount>0){
            flag = true;
        }
        return flag;
    }
}
